/*
 * @Author: nqx2007 fenglimima334@163.com
 * @Date: 2023-12-07 21:37:00
 * @LastEditors: nqx2007 fenglimima334@163.com
 * @LastEditTime: 2023-12-11 20:53:49
 * @FilePath: /operator-manager/src/utils/permission.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

export function matchMultiplePerms(permissions: string[], value: string[]) {
  if (value && value instanceof Array && value.length > 0) {
    const permissionDatas = value;
    const all_permission = '*:*:*';
    const hasPermission = permissions.some((permission) => {
      return (
        all_permission === permission || permissionDatas.includes(permission)
      );
    });
    if (!hasPermission) {
      return false;
    }
    return true;
  }
  console.error('需要输入权限access进行判断');
  return false;
}

export function matchSinglePerm(permissions: string[], value: string) {
  if (value && value.length > 0) {
    const permissionDatas = value;
    const all_permission = '*:*:*';
    const hasPermission = permissions.some((permission) => {
      return all_permission === permission || permissionDatas === permission;
    });
    if (!hasPermission) {
      return false;
    }
    return true;
  }
  console.error('需要输入权限access进行判断');
  return false;
}

export function matchPermission(
  permissions: string[] | undefined,
  value: any,
): boolean {
  if (permissions === undefined) return false;
  const type = typeof value;
  if (type === 'string') {
    return matchSinglePerm(permissions, value);
  }
  return matchMultiplePerms(permissions, value);
}
